package tools

import (
	"context"
	"fmt"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var client *mongo.Client

const (
	DB_NAME = "go_db"
)

func InitDB() {
	// 设置客户端连接配置
	//clientOptions := options.Client().ApplyURI("mongodb://10.12.159.71:27017")
	clientOptions := options.Client().ApplyURI("mongodb://192.168.190.143:27017")
	// 连接到MongoDB
	var err error
	client, err = mongo.Connect(context.TODO(), clientOptions)
	if err != nil {
		fmt.Println(err)
	}
	// 检查连接
	err = client.Ping(context.TODO(), nil)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Connected to MongoDB!")
}

func GetMongoClient() *mongo.Client {
	return client
}

func GetCollection(name string) *mongo.Collection {
	return client.Database("go_db").Collection(name)
}
